package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/brandController")
public class BrandController {

    @DubboReference
    private BrandService brandService;


    //查询全部brand数据
    @RequestMapping("findAll")
    @ResponseBody
    public AjaxResult findAll(String name) {
        if (name == null && "".equals(name)){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            List<TbBrand> all = brandService.findAll(name);
            return AjaxResult.OK().setData(all);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //增加brand数据
    @RequestMapping("addBrand")
    @ResponseBody
    public AjaxResult addBrand (TbBrand brand) {
        try {
            brandService.addBrand(brand);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除brand
    @RequestMapping("deleteBrand")
    @ResponseBody
    public AjaxResult deleteBrand(Long[] ids){
        try {
            brandService.deleteBrand(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //回显数据
    @RequestMapping("editRole")
    @ResponseBody
    public AjaxResult editRole(Integer id){

        try {
            TbBrand brand = brandService.getById(id);
            return AjaxResult.OK().setData(brand);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改数据
    @RequestMapping("updateBrand")
    @ResponseBody
    public AjaxResult updateBrand(TbBrand brand){
        try {
            brandService.updateBrand(brand);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
